package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.elinkway.infinitemovies.bean.Subscription;
import com.elinkway.infinitemovies.bean.SubscriptionMainData;
import com.elinkway.infinitemovies.ui.activity.SubscriptionActivity;
import com.elinkway.infinitemovies.utils.LogUtils;
import com.elinkway.infinitemovies.view.CircleImageView;
import com.elinkway.infinitemovies.view.EqualRatioImageView;
import com.le123.ysdq.R;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by zhaopeipei on 2016/9/2.
 */
public class SubscriptionListAdapter  extends BaseAdapter {

    private static String TAG = "SubscriptionListAdapter";

    private static final String HEAD_IMAGE = "head_image";

    private Context mContext;

    private LayoutInflater inflater;

    private Subscription item_data;

    private class ListItemHolder {
        CircleImageView mHeadImage;
        TextView mTitle;
        TextView mAttentionNumber;
        EqualRatioImageView mFirstImageView;
        TextView mFirstImageTitle;
        EqualRatioImageView mSecondImageView;
        TextView mSecondImageTitle;
        LinearLayout mLinearSubscriptionInfo;
    }

    public SubscriptionListAdapter(Context context, Subscription result) {
        super();
        mContext = context;
        inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        item_data = result;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        ListItemHolder item;
        final SubscriptionMainData subscriptionMainData = item_data.getSubscription().get(position);
        if (convertView == null) {
            LogUtils.e("", "convert VIew");
            convertView = inflater.inflate(R.layout.subscription_item, null);
            item = new ListItemHolder();
            item.mLinearSubscriptionInfo = (LinearLayout) convertView.findViewById(R.id.ll_subscription_info);
            item.mHeadImage = (CircleImageView) convertView.findViewById(R.id.subscription_header_icon_iv);
            item.mTitle = (TextView) convertView.findViewById(R.id.subscription_title);
            item.mAttentionNumber = (TextView) convertView.findViewById(R.id.subscription_attention_number);
            item.mFirstImageView = (EqualRatioImageView) convertView.findViewById(R.id.subscription_first_item_poster);
            item.mFirstImageTitle = (TextView) convertView.findViewById(R.id.tv_first_item_name);
            item.mSecondImageView = (EqualRatioImageView) convertView.findViewById(R.id.subscription_second_item_poster);
            item.mSecondImageTitle = (TextView) convertView.findViewById(R.id.tv_second_item_name);
            convertView.setTag(item);
        } else {
            LogUtils.e("", "convert VIew no ");
            item = (ListItemHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(subscriptionMainData.getIcon(), item.mHeadImage);
        item.mTitle.setText(subscriptionMainData.getName());
        if (subscriptionMainData.getAlbumList() != null &&
                subscriptionMainData.getAlbumList().size() > 0) {
            ImageLoader.getInstance().displayImage(subscriptionMainData.getAlbumList().get(0)
                    .getVideoList().get(0).getImage(), item.mFirstImageView);
            item.mFirstImageTitle.setText(subscriptionMainData.getAlbumList().get(0)
                    .getVideoList().get(0).getName());
        }
        item.mLinearSubscriptionInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, SubscriptionActivity.class);
                intent.putExtra(HEAD_IMAGE, subscriptionMainData.getIcon());
                mContext.startActivity(intent);
            }
        });
        LogUtils.e(TAG, "get view pos is " + position);
        return convertView;
    }

    @Override
    public int getCount() {
        return item_data.getSubscription().size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItemData(Subscription moreData) {
        if (item_data == null) {
            item_data = moreData;
        } else {
            item_data.getSubscription().addAll(moreData.getSubscription());
        }
    }

    public void setItemData(Subscription result) {
        item_data = result;
    }

    public Subscription getItemData() {
        return item_data;
    }
}
